/* eslint-disable consistent-return */
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable prefer-const */
/* eslint-disable no-plusplus */
/* eslint-disable no-param-reassign */
/* eslint-disable object-shorthand */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable react-hooks/rules-of-hooks */
/*
 * @Author: 李家磊
 * @Date: 2022-05-05 11:41:34
 * @LastEditTime: 2022-06-02 11:42:46
 * @LastEditors: 李家磊
 * @Description:
 * @FilePath: /temp/src/pages/file/index.tsx
 */

import { Button, Input, Upload } from "antd";
import { UploadChangeParam, UploadProps } from "antd/lib/upload";
import { useEffect, useState } from "react";
import { request } from "umi";


interface fileProps {
  uid: string,
  name: string,
  url: string,
  size: number,
  type: string
}

const test: React.FC = () => {
  const [fileList, setFileList] = useState<fileProps[]>([]);

  const getFileList = async () => {
    let result =  await request("http://localhost:9000/file/list");
    let tmpArr: fileProps[] = [];
    if(result){
      for(let i = 0 ; i < result.length ; i++){
        tmpArr[i] = {
          uid: result[i]["fileId"],
          name: result[i]["fileName"],
          url: result[i]["fileUrl"],
          size: result[i]["fileLen"],
          type: result[i]["extName"]
        }
      }
      setFileList(tmpArr);
      return tmpArr;
    }
  }

  const removeFile = async(uid: string) => {
    let result = await request("http://localhost:9000/file/delete",{
      method: 'POST',
      params: {fileId: uid}
    });
    console.log(result);
    return result;
  }

  useEffect(()=>{
  //   let isUnmount = true;
    getFileList();
  //   return () => {isUnmount = false};
  },[])


  const props: UploadProps = {
    action: 'http://localhost:9000/file/upload',
    onChange({file, fileList}){
      if (file.status !== 'uploading'){
        getFileList();
      }
    },
    fileList: fileList,
    onRemove:(fileList)=>{
      removeFile(fileList.uid);
      getFileList();
    }
  }


  return (
    <>
      <Upload
        {...props}
      >
        <Button>
            上传文件
          </Button>
      </Upload>


      </>
  )
}

export default test
